/**
 * Copyright 2002 Cordys R&D B.V. 
 * 
 * This file is part of the Cordys .NET Connector. 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 //@Author	: Lejil 
//@Class	: JNetUnManaged

#pragma once
#include "stdafx.h"
#include "JNetUnManaged.h"
#include "ManagedCallHandler.h"
#include "Transaction.h"
#include "CTSToNative.h"

//    JNI Method Implementation for, Generated JNI Method
//		This is the unmanged entry point
//     uses #pragma unmanaged to mark this function as an unmanaged .
#pragma unmanaged
JNIEXPORT jboolean JNICALL Java_com_eibus_applicationconnector_dotnet_Transaction_Invoke
  (JNIEnv *env, jobject object, jint request, jint response, jint implementation)
{
	return JNetUnManaged::InvokeManaged(request,response,implementation);
	env=NULL;
}


//	Managed Entry Point
//		Create the Instance
//		Initialise the values
//		Invoke the desired .NET method
#pragma managed
bool JNetUnManaged::InvokeManaged(int request,int response,int implementation)
{
	bool isExecuted							=	false;
	ManagedCallHandler __gc *dotNETHandler	=	NULL;
	NativeToCTS __gc *nativeToCTS			=	NULL;
	try
	{
		nativeToCTS			=	new NativeToCTS(request,implementation);
		
		dotNETHandler		=	new ManagedCallHandler(nativeToCTS->Parameter);
		
		isExecuted			=	dotNETHandler->Invoke(response);
	
	}
	catch(FileNotFoundException *fnfEx)
	{
		return isExecuted;
	}
	catch(FormatException *fmEx)
	{
		CTSToNative::CreateSoapFault(response,"Arguments.Type-Mismatch",fmEx->Message,fmEx->ToString());
		return isExecuted;
	}
	catch(Exception *ex)
	{
		CTSToNative::CreateSoapFault(response,"Server.NotImplemented",ex->Message,ex->ToString());
		return isExecuted;
	}
	__finally
	{
		if (nativeToCTS != NULL)
			delete nativeToCTS;
		if (dotNETHandler != NULL)
			delete dotNETHandler;
	}
	return isExecuted;
}
